package com.deer.wms.inventory.web;


import com.deer.wms.intercept.annotation.User;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.inventory.model.Inventory.ItemBatch;
import com.deer.wms.inventory.model.Inventory.ItemBatchCriteria;
import com.deer.wms.inventory.model.Inventory.ItemBatchDto;
import com.deer.wms.inventory.service.ItemBatchService;
import com.deer.wms.project.root.core.result.CommonCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
* Created by guo on 2019/12/02.
*/
@Api(description = "物料批次信息api接口")
@RestController
@RequestMapping("/item/batchs")
public class ItemBatchController {

    @Autowired
    private ItemBatchService itemBatchService;

    @ApiOperation(value = "添加物料批次信息", notes = "添加物料批次信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
    })
    @PostMapping("/insert")
    public Result add(@RequestBody ItemBatch itemBatch, @ApiIgnore @User CurrentUser currentUser) {
        if(currentUser == null){
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        itemBatchService.createItemBatch(itemBatch);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/delete")
    public Result delete(Integer id) {
        itemBatchService.removeById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody ItemBatch itemBatch) {
        itemBatchService.updateById(itemBatch);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/detail")
    public Result detail(Integer id) {
        ItemBatch itemBatch = itemBatchService.getById(id);
        return ResultGenerator.genSuccessResult(itemBatch);
    }

    @GetMapping("/list")
    public Result list(ItemBatchCriteria criteria) {
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<ItemBatchDto> list = itemBatchService.findList(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @PostMapping("/findByOrganizationIdAndItemCode")
    public Result findByOrganizationIdAndItemCode(Integer organizationId,String itemCode) {
        itemBatchService.findByOrganizationIdAndItemCode(organizationId,itemCode);
        return ResultGenerator.genSuccessResult();
    }

}
